package com.chen.blog.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chen.blog.common.api.CommonResult;
import com.chen.blog.common.component.ImgUtil;
import com.chen.blog.common.constant.MessageConstant;
import com.chen.blog.config.security.component.AuthenticationUtil;
import com.chen.blog.model.User;
import com.chen.blog.service.UserService;
import com.chen.blog.vo.LoginVo;
import com.chen.blog.vo.PwdVo;
import com.chen.blog.vo.ResetPwdVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.security.Principal;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author chen
 * @since 2022-01-24
 */
@Api(tags = "用户管理 UserController")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private AuthenticationUtil authenticationUtil;

    @Autowired
    private ImgUtil imgUtil;

    @ApiOperation("新增用户")
    @PostMapping("/add")
    public CommonResult add(@RequestBody User user) {
        return userService.add(user);
    }

    @ApiOperation("删除用户")
    @DeleteMapping("/{ids}")
    public CommonResult delete(@PathVariable Integer[] ids) {
        return userService.deleteByIds(ids);
    }

    @ApiOperation("更新用户")
    @PutMapping
    public CommonResult update(@RequestBody User user) {
        return userService.updateByParam(user);
    }

    @ApiOperation("更新密码")
    @PutMapping("/pwd")
    public CommonResult updatePwd(@RequestBody PwdVo pwdVo) {
        return userService.updatePwd(pwdVo);
    }

    @ApiOperation("重置密码")
    @PutMapping("/resetPwd")
    public CommonResult resetPwd(@RequestBody ResetPwdVo resetPwdVo) {
        return userService.resetPwd(resetPwdVo);
    }

    @ApiOperation("发送验证码")
    @GetMapping("/{email}")
    public CommonResult sendCode(@PathVariable String email) {
        return userService.sendCode(email);
    }

    @ApiOperation("更新头像")
    @PostMapping("/pic/{id}")
    public CommonResult updatePic(@RequestParam("file") MultipartFile file, @PathVariable Integer id) {
        return imgUtil.updatePic(file, id, userService.getById(id));
    }

    @ApiOperation("检索用户")
    @GetMapping("/{currentPage}/{pageSize}")
    public CommonResult get(@PathVariable(value = "currentPage") Integer currentPage, @PathVariable(value = "pageSize") Integer pageSize,
                            User user) {
        return userService.getByParamAndPage(currentPage, pageSize, user);
    }

    @ApiOperation("获取指定id的用户")
    @GetMapping("/info/{id}")
    public User getById(@PathVariable Integer id) {
        return userService.getById(id);
    }

    @ApiOperation("用户登录")
    @PostMapping("/login")
    public CommonResult login(@RequestBody LoginVo loginVo) {
        if (userService.getOne(new QueryWrapper<User>().eq("username", loginVo.getUsername())) == null) {
            return CommonResult.error(MessageConstant.ADMIN_LOGIN_FAIL);
        }
        return authenticationUtil.login(loginVo);
    }

    @ApiOperation("获取当前登录用户详情")
    @GetMapping("/detail")
    public User detail() {
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (null == user) {
            return null;
        }
        user.setPassword(null);
        return user;
    }

}
